<?php
namespace Admin\Controller;
use Think\Controller;
use Org\Net\Http;
class ModuleController extends Controller {
	public function test(){
// 		$module = D("ModuleView");
// 		$data['id'] ="MA71551229380761";
// 		$data['name'] ="测试name";
// 		$data['subtitle'] ="测试subtitle";
// 		$data['image'] ="测试image";
// 		$data['city'] =66;
// 		var_dump($module->add($data));
		$test = '';
		echo json_encode($test).'<br/>';
		$test = array();
		echo json_encode($test).'<br/>';
	}
	/**
	 * 获取模块列表
	 */
    public function index(){
    	$module = D("ModuleView");
    	$list = $module->select();
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 二级联动——根本所选的城市获取相对于的景区、酒店、餐饮等
     */
    public function query(){
    	$city_id = I("id");
    	$list = S("".$city_id);
    	if(!$list){
	    	$scenic = D("Scenic");
	    	$sce = $scenic->where(array('Scenic.city'=>$city_id))->select();
	    	$list['scenic'] = $sce;
	    	$diet = D("Diet");
	    	$die = $diet->where(array('Diet.city'=>$city_id))->select();
	    	$list['diet'] = $die;
	    	$hotel = D("Hotel");
	    	$hot = $hotel->where(array('Hotel.city'=>$city_id))->select();
	    	$list['hotel'] = $hot;
	    	S("".$city_id,$list);
    	}
    	$this->ajaxReturn($list,'json');
    }
    /**
     * 添加模块
     */
    public function add(){
    	if(IS_POST){
    		$module = D("Module");
    		$data = $module->create($_POST);
    		$scenics = $_POST['scenics'] != ''?$_POST['scenics']:array();
    		$diets = $_POST['diets'] != ''?$_POST['diets']:array();
    		$hotels = $_POST['hotels'] != ''?$_POST['hotels']:array();
    		
    		$data['scenics'] = json_encode($scenics);
    		$data['diets'] = json_encode($diets);
    		$data['hotels'] = json_encode($hotels);
    		$result = $module->add($data);
    		if($result){
    			$this->success("添加成功",U("Module/index"));
    		}else{
    			$this->error("添加失败");
    		}
    	}else{
    		$city = D('City');
    		$arr = $city->select();
    		$this->assign('citys',$arr);
    		$this->display();
    	}
    }
    /**
     * 删除模块
     */
    public function delete(){
    	$where['id'] = I('id');
    	$result = M('Module')->where($where)->delete();
    	if($result){
    		$this->success("删除成功",U("Module/index"));
    	}else{
    		$this->error("删除失败");
    	}
    }
}